/*
* Copyright 2004-2010 mb_sx@hotmail.com
* All Rights Reserved.
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DeskNotes
{
	/// <summary>
	/// RemindersForm displays missed reminders from desknotes. 
	/// Missed are those when user did not interact with notify form.
	/// Reminders form is being open from DeskNotes class. 
	/// Note then registers its RemindersActionHandler
	/// to receive event about action user did for displayed desknote.
	/// Handler is implemented inside NoteForm for complexity :-) and for easy access
	/// to NoteForm UI elements like reminder toolbar.
	/// </summary>
	public class RemindersForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnOpen;
		private System.Windows.Forms.Button btnDismissAll;
		private System.Windows.Forms.Button btnDismiss;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.ListView listReminders;
		private System.Windows.Forms.ColumnHeader text;
		private System.Windows.Forms.ColumnHeader reminder;
		private System.Windows.Forms.ImageList imagesList;
        private HelpProvider helpProvider1;
		private System.ComponentModel.IContainer components;

		//private DeskNotes deskNotes = null;

		
		/// <summary>
		/// Closed - form closed no action for desknotes selected
		/// Dismiss - dismiss reminder from form and DeskNote
		/// Open - activate given desknote
		/// </summary>
		internal enum Action : short 
		{
			Closed, 
			Dismiss,
			Open
		}

		public RemindersForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			
		}

		internal RemindersForm(DeskNotesData deskNotes)
		{
			InitializeComponent();
			//this.deskNotes = deskNotes;
			this.Disposed +=new EventHandler(deskNotes.RemindersDisposed);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RemindersForm));
            this.label1 = new System.Windows.Forms.Label();
            this.btnOpen = new System.Windows.Forms.Button();
            this.btnDismissAll = new System.Windows.Forms.Button();
            this.btnDismiss = new System.Windows.Forms.Button();
            this.btnClose = new System.Windows.Forms.Button();
            this.listReminders = new System.Windows.Forms.ListView();
            this.text = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.reminder = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.imagesList = new System.Windows.Forms.ImageList(this.components);
            this.helpProvider1 = new System.Windows.Forms.HelpProvider();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            resources.ApplyResources(this.label1, "label1");
            this.label1.Name = "label1";
            // 
            // btnOpen
            // 
            resources.ApplyResources(this.btnOpen, "btnOpen");
            this.btnOpen.Name = "btnOpen";
            this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
            // 
            // btnDismissAll
            // 
            resources.ApplyResources(this.btnDismissAll, "btnDismissAll");
            this.btnDismissAll.Name = "btnDismissAll";
            this.btnDismissAll.Click += new System.EventHandler(this.btnDismissAll_Click);
            // 
            // btnDismiss
            // 
            resources.ApplyResources(this.btnDismiss, "btnDismiss");
            this.btnDismiss.Name = "btnDismiss";
            this.btnDismiss.Click += new System.EventHandler(this.btnDismiss_Click);
            // 
            // btnClose
            // 
            resources.ApplyResources(this.btnClose, "btnClose");
            this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnClose.Name = "btnClose";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // listReminders
            // 
            resources.ApplyResources(this.listReminders, "listReminders");
            this.listReminders.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.text,
            this.reminder});
            this.listReminders.FullRowSelect = true;
            this.listReminders.GridLines = true;
            this.listReminders.Name = "listReminders";
            this.listReminders.SmallImageList = this.imagesList;
            this.listReminders.UseCompatibleStateImageBehavior = false;
            this.listReminders.View = System.Windows.Forms.View.Details;
            this.listReminders.SelectedIndexChanged += new System.EventHandler(this.listReminders_SelectedIndexChanged);
            this.listReminders.DoubleClick += new System.EventHandler(this.listReminders_DoubleClick);
            // 
            // text
            // 
            resources.ApplyResources(this.text, "text");
            // 
            // reminder
            // 
            resources.ApplyResources(this.reminder, "reminder");
            // 
            // imagesList
            // 
            this.imagesList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imagesList.ImageStream")));
            this.imagesList.TransparentColor = System.Drawing.Color.Transparent;
            this.imagesList.Images.SetKeyName(0, "");
            // 
            // helpProvider1
            // 
            resources.ApplyResources(this.helpProvider1, "helpProvider1");
            // 
            // RemindersForm
            // 
            resources.ApplyResources(this, "$this");
            this.CancelButton = this.btnClose;
            this.Controls.Add(this.listReminders);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnDismiss);
            this.Controls.Add(this.btnDismissAll);
            this.Controls.Add(this.btnOpen);
            this.Controls.Add(this.label1);
            this.helpProvider1.SetHelpKeyword(this, resources.GetString("$this.HelpKeyword"));
            this.helpProvider1.SetHelpNavigator(this, ((System.Windows.Forms.HelpNavigator)(resources.GetObject("$this.HelpNavigator"))));
            this.Name = "RemindersForm";
            this.helpProvider1.SetShowHelp(this, ((bool)(resources.GetObject("$this.ShowHelp"))));
            this.Closing += new System.ComponentModel.CancelEventHandler(this.RemindersForm_Closing);
            this.Shown += new System.EventHandler(this.RemindersForm_Shown);
            this.ResumeLayout(false);

		}
		#endregion
		/// <summary>
		/// Adds desknote into reminders list box.
		/// </summary>
		/// <param name="note">Note to list as missed reminder and being notified when user interacts with list.</param>
		internal ListViewItem AddNote(Note note)
		{
			System.Text.StringBuilder text = new System.Text.StringBuilder( 50 );
            string t = note.PlainText;
            if (t.Length > 50 )
			{
				text.Insert(0, t.Substring(0, 47));
				text.Append("...");
			}
			else 
				text.Insert(0, t);

			ListViewItem noteItem = new ListViewItem( text.ToString().Replace( "\n", " " ));
			noteItem.SubItems.Add( note.AlarmText );
			noteItem.Tag = note.MissedRemindersHandler;
			noteItem.ImageIndex = 0;
			listReminders.Items.Add( noteItem );
			if (this.Visible)
				this.Activate();
			else
				this.Show();
			return noteItem;
		}

		/// <summary>
		/// Remove note from missed reminders window
		/// </summary>
		/// <param name="noteItem">note item existing in window</param>
		internal void RemoveNote(ListViewItem noteItem)
		{
			noteItem.Tag = null;
			listReminders.Items.Remove(noteItem);
			if (listReminders.Items.Count == 0)
				this.Close();
		}

		/// <summary>
		/// When form is closed inform Desknotes about it.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void RemindersForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			//Send messages to all notes 
			foreach (ListViewItem item in listReminders.Items)
			{
				RemindersActionHandler handler = (RemindersActionHandler)item.Tag;
				item.Tag = null;
				handler(this, new RemindersActionEventArgs(Action.Closed));
			}
		}

		/// <summary>
		/// Open selected reminded desknote
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnOpen_Click(object sender, System.EventArgs e)
		{
			foreach( ListViewItem item in listReminders.SelectedItems)
			{
				RemindersActionHandler handler = (RemindersActionHandler)item.Tag;
                if (null != handler)
                {
                    handler(this, new RemindersActionEventArgs(Action.Open));
                }
			}
		}
		/// <summary>
		/// Remove all reminders from desknotes in list of missed reminders
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDismissAll_Click(object sender, System.EventArgs e)
		{
			foreach( ListViewItem item in listReminders.Items)
			{
				RemindersActionHandler handler = (RemindersActionHandler)item.Tag;
				item.Tag = null;
				handler(this, new RemindersActionEventArgs(Action.Dismiss));
			}
		}

		/// <summary>
		/// Remove missed reminders from selected desknotes
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDismiss_Click(object sender, System.EventArgs e)
		{
			foreach( ListViewItem item in listReminders.SelectedItems)
			{
				RemindersActionHandler handler = (RemindersActionHandler)item.Tag;
				item.Tag = null;
				handler(this, new RemindersActionEventArgs(Action.Dismiss));
			}
		}

		/// <summary>
		/// Activate selected Desknote
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listReminders_DoubleClick(object sender, System.EventArgs e)
		{
			foreach( ListViewItem item in listReminders.SelectedItems)
			{
				RemindersActionHandler handler = (RemindersActionHandler)item.Tag;
				handler(this, new RemindersActionEventArgs(Action.Open));
				//we need just one item, quit now. Not a nice code, but better than enumeration
				return;
			}
		}
		/// <summary>
		/// Enables/disables buttons when there are selected or no selected items.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listReminders_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(listReminders.SelectedItems.Count > 0)
			{
				this.btnDismiss.Enabled = true;
				this.btnOpen.Enabled = true;
			} 
			else 
			{
				this.btnDismiss.Enabled = false;
				this.btnOpen.Enabled = false;
			}
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

        private void RemindersForm_Shown(object sender, EventArgs e)
        {
            listReminders.Items[0].Selected = true;
            listReminders.Select();
        }

	}

	/// <summary>
	/// Event args send to handler contains what action taken for what reminders.
	/// </summary>
	internal class RemindersActionEventArgs : System.EventArgs
	{
		/// <summary>
		/// 
		/// </summary>
		private readonly RemindersForm.Action action;
		//private readonly long[] ids;

		/// <summary>
		/// Retrieve what user wants to do with missed reminder.
		/// </summary>
		internal RemindersForm.Action PostReminderAction
		{
			get
			{
				return action;
			}
		}

		/// <summary>
		/// Event content with action for note affected by action
		/// </summary>
		/// 
		internal RemindersActionEventArgs( RemindersForm.Action action )
		{
			this.action = action;
		}
	}
	/// <summary>
	/// Class interested in receiving feedback from missedrReminders form registers for this event.
	/// </summary>
	internal delegate void RemindersActionHandler( object sender, RemindersActionEventArgs args );
}
